package edu.gdin.ecommerce.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.DicDetail;
import edu.gdin.ecommerce.model.ParamDetail;
import edu.gdin.ecommerce.model.Role;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.IDicService;
import edu.gdin.ecommerce.service.IParamService;
import edu.gdin.ecommerce.service.IRoleService;
import edu.gdin.ecommerce.service.IUserService;
import edu.gdin.ecommerce.util.Pager;

@Component("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2624514663000155191L;

	private IUserService userService;
	private IRoleService roleService;
	private IDicService dicService;
	private IParamService paramService;
	private List<Role> roles;
	private List<Integer> roleIds;
	private User user;
	private Pager pager;
	
	private Integer currentPage = 1;
	private String oldPwd;
	private String newPwd;
	private String confirmPwd;
	private String account = "";
	private String msg = "";
	
	
	
	
	public IUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public IRoleService getRoleService() {
		return roleService;
	}
	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public IDicService getDicService() {
		return dicService;
	}
	@Resource
	public void setDicService(IDicService dicService) {
		this.dicService = dicService;
	}
	public IParamService getParamService() {
		return paramService;
	}
	@Resource
	public void setParamService(IParamService paramService) {
		this.paramService = paramService;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	public List<Integer> getRoleIds() {
		return roleIds;
	}
	public void setRoleIds(List<Integer> roleIds) {
		this.roleIds = roleIds;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public String getOldPwd() {
		return oldPwd;
	}
	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}
	public String getNewPwd() {
		return newPwd;
	}
	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
	public String getConfirmPwd() {
		return confirmPwd;
	}
	public void setConfirmPwd(String confirmPwd) {
		this.confirmPwd = confirmPwd;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
	
	// ----------  methods  -----------
	
	public String login() {
		Map<String, Object> session = ActionContext.getContext().getSession(); 
		User user = this.userService.login();
		session.put("user", user);
		
		Map<String, Object> application = ActionContext.getContext().getApplication();
		if(application.get("dics") == null) {
			List<DicDetail> dicDetails = this.dicService.getAllDicDetails();
			
			Map<String, Map<String, String>> dicMap = new HashMap<String, Map<String,String>>();
			for (DicDetail d : dicDetails) {
				String dicTypeId = d.getDicType().getDicTypeId();
				if(dicMap.containsKey(dicTypeId)) {
					dicMap.get(dicTypeId).put(d.getValue(), d.getName());
				} else {
					Map<String, String> detail = new HashMap<String, String>();
					
					detail.put(d.getValue(), d.getName());
					dicMap.put(dicTypeId, detail);
				}
			}
			
			Map<String, List<DicDetail>> dics = new HashMap<String, List<DicDetail>>();
			for (DicDetail d : dicDetails) {
				String dicTypeId = d.getDicType().getDicTypeId();
				if(dics.containsKey(dicTypeId)) {
					dics.get(dicTypeId).add(d);
				} else {
					List<DicDetail> details = new ArrayList<DicDetail>();
					details.add(d);
					dics.put(dicTypeId, details);
				}
			}
			
			application.put("dicMap", dicMap);
			application.put("dics", dics);
		}
		
		if(application.get("params") == null) {
			List<ParamDetail> details = this.paramService.getAllParamDetails();
			
			Map<String, Object> params = new HashMap<String, Object>();
			for (ParamDetail d : details) {
				params.put(d.getParamDetailId(), d.getValue());
			}
			application.put("params", params);
		}
		
		return "login";
	}
	
	/**
	 * 密码未加密！
	 * @return
	 */
	public String changePwd() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		if(!this.newPwd.equals(this.confirmPwd)) {
			this.addFieldError("error", "新密码不一致！");
			return "changePwd";
		}
		if(user == null || !user.getPassword().equals(this.oldPwd)) {
			this.addFieldError("error", "密码错误！");
			return "changePwd";
		}
		if(!this.userService.changePwd(user, this.newPwd)) {
			this.addFieldError("error", "密码修改失败！");
			return "changePwd";
		}
		
		// 不需刷新session，也能更新登录用户的密码?? (session自动同步)
		//user.setPassword(newPwd);
		//session.put("user", user);
		this.msg = "update password success";
		this.addFieldError("error", "密码修改成功！");
		
		return "index";
		
	}
	
	
	public String queryUser() {
		this.pager = this.userService.query(this.account, this.currentPage);
		if(this.pager.getList().size() == 0) {
			this.addFieldError("msg", "没找到符合条件的用户！");
		}
		return "queryUser";
	}

	
	public String preAddUser() {
		this.roles = this.roleService.getAllRoles();
		return "preAddUser";
	}
	
	
	public String addUser() {
		if(this.userService.isExist(this.user)) {
			this.addFieldError("error", "存在同名账户！");
			return "preAddUser";
		}
		
		this.roles = this.roleService.getAllRoles();
		for(Integer i : this.roleIds) {
			for (Role r : this.roles) {
				if (r.getRoleId().equals(i)) {
					this.user.getRoles().add(r);
				}
			}
		}
		this.user.setPassword(this.user.getAccount());
		this.user.setStatus(0); // 0=正常 1=停用
		this.user.setLastTime(new Date());
		this.userService.addUser(this.user);
		
		return "addUser";
	}
	
	
	public String preEditUser() {
		this.roles = this.roleService.getAllRoles();
		this.user = this.userService.getUserUserByAccount(user.getAccount());
		
		return "preEditUser";
	}
	
	
	public String editUser() {
		this.roles = this.roleService.getAllRoles();
		this.userService.editUser(this.user, this.roles, this.roleIds);
		
		return "editUser";
	}
	
	
	public String changeUserStatus() {
		this.userService.changeUserStatus(this.user);
		return "changeStatus";
	}
	
	
}
